knitr::opts_chunk$set(echo = T, message = F, warning = F)
library(tidyverse)
library(ggbeeswarm)
library(ggdist)
library(waffle)
library(bayestestR)
library(see)
library(ggpubr)

Generating data

# how many participants should be simulated per group?
sim_n <- 250

# what should the effect size be (in SD)
sim_es <- .5

# what is the intervention and control?
label_intervention <- "reading\non tablet"
label_control      <- "reading\non paper"

# what is measured as dependent variable? Add a label as character
dv_label <- "Score on text comprehension test"

# what is the scale of the dependent variable? Add 5 labels as character
dv_scale <- c("10", "20", "30", "40", "50")

# p_data <- data.frame(x = c(rnorm(sim_n, mean = .5, sd = 1), rnorm(sim_n, mean = 0, sd = 1)),
#                      group = factor(rep(c("With Intervention", "Without Intervention"), each = sim_n), levels = c("Without Intervention", "With Intervention")))

# generate near perfect normally distributed data
p_data <- data.frame(x = c(distribution_normal(n = sim_n, mean = sim_es, sd = 1), 
                           distribution_normal(n = sim_n, mean = 0, sd = 1)),
                     group = factor(rep(c("With Intervention", "Without Intervention"), each = sim_n), 
                                    levels = c("Without Intervention", "With Intervention")))

# generate plots to label means
mean_label <-
  ggplot(data.frame(x=1:2, group = factor(c("With Intervention", "Without Intervention"))), aes(group = group, fill = group)) +
  geom_segment(aes(x=0, y=0, xend=0, yend=.1), color = "#440154", size = 3) +
  geom_segment(aes(x=sim_es, y=0, xend=sim_es, yend=.1), color = '#7e7312', size = 3) +
  geom_label(x=c(0,sim_es), y=0, aes(label=c(paste0("mean:\n", label_control), paste0("mean:\n", label_intervention))), 
             color = c('white', '#090c0e'),
             fontface = "italic", lineheight = 0.7) +
  scale_fill_viridis_d() +
  scale_color_viridis_d() +
  # geom_label(x=sim_es, y=0, label=paste0("mean:\n", label_intervention), color = '#090c0e', fill = "#fde725",  
  #            fontface = "italic", lineheight = 0.7) +
  scale_y_continuous(limits = c(-.05,.1), expand = c(0,0)) +
  scale_x_continuous(breaks = c(-2:+2), labels = dv_scale, limits = c(-3,3.5)) +
  xlab(dv_label) +
  # theme_minimal() +
  theme_modern() +
  theme(legend.position = "none",
        axis.ticks.x = element_blank(),
        axis.line.x = element_blank(),
        axis.line.y = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.text.x = element_blank()
        )

Normal Curves

Normal Curve (using the simulated data) with mean as dashed line

ggplot(p_data, aes(x=x, group = group, fill = group, colour = group)) +
  # geom_dots() +
  geom_vline(aes(xintercept = 0), color = "#440154", linetype = "dashed", size = 1.5) +
  geom_density(alpha = .6, size = 3) +
  geom_vline(aes(xintercept = sim_es), color = "#fde725", linetype = "dashed", size = 1.5) +
  scale_fill_viridis_d() +
  scale_color_viridis_d() +
  geom_label(x=-2, y=.3, label=label_control, color = 'white', fill = "#440154", fontface = "bold") +
  geom_label(x=2.2, y=.3, label=label_intervention, color = '#090c0e', fill = "#fde725", fontface = "bold") +
  scale_y_continuous(expand = c(0,0)) +
  scale_x_continuous(breaks = c(-2:+2), labels = dv_scale, limits = c(-3,3.5)) +
  xlab(dv_label) +
  ylab("count") +
  theme_modern() +
  theme(legend.position = "none",
        # panel.grid = element_blank(),
        # axis.ticks.x = element_line(color = "#666666"),
        # axis.line.x = element_line(color = "#666666"),
        # axis.line.y = element_blank(),
        # axis.title.y = element_blank(),
        # axis.title.x = element_blank(),
        axis.text.y = element_blank(),
        # axis.text.x = element_text(face = "bold")
        )

Violin plot

ggplot(p_data, aes(x=1, y = x, group = group, fill = group, colour = group)) +
  geom_hline(aes(yintercept = 0), color = "#440154", linetype = "dashed", size = 1.5) +
  geom_violin(alpha = .7, position = position_dodge(width = 0), size = 3) +
  geom_hline(aes(yintercept = sim_es), color = "#fde725", linetype = "dashed", size = 1.5) +
  scale_fill_viridis_d() +
  scale_color_viridis_d() +
  geom_label(x=1.17, y=-1.5, label=label_control, color = 'white', fill = "#440154", fontface = "bold") +
  geom_label(x=1.17, y=1.5+sim_es, label=label_intervention, color = '#090c0e', fill = "#fde725", fontface = "bold") +
  scale_y_continuous(expand = c(0,0), breaks = c(-2:+2), labels = dv_scale) +
  ylab(dv_label) +
  xlab("count") +
  theme_modern() +
  theme(legend.position = "none",
        # panel.grid = element_blank(),
        # axis.ticks.x = element_line(color = "#666666"),
        # axis.line.x = element_line(color = "#666666"),
        # axis.line.y = element_blank(),
        # axis.title.y = element_blank(),
        # axis.title.x = element_blank(),
        axis.text.y = element_blank(),
        # axis.text.x = element_text(face = "bold")
        ) +
  coord_flip()

Beeswarm

geom_beeswarm() overlay

ggplot(p_data%>%dplyr::filter(group == "Without Intervention"), aes(x = 1, y = x)) +
  geom_beeswarm(alpha = .75, fill = "#440154", color = "#440154", 
                size = 6, priority = "random", cex = 2) +
  geom_beeswarm(data = p_data%>%dplyr::filter(group == "With Intervention"), 
                alpha = .8, fill = "#fde725", color = "#edd926", # color was #fde725
                size = 6, priority = "random", cex = 2) +
  geom_hline(aes(yintercept = 0), color = "#440154", linetype = "dashed", size = 2, alpha = .7) +
  geom_hline(aes(yintercept = sim_es+.01), color = "darkgrey", linetype = "dashed", size = 2, alpha = .8) +
  geom_hline(aes(yintercept = sim_es-.01), color = "darkgrey", linetype = "dashed", size = 2, alpha = .8) +
  geom_hline(aes(yintercept = sim_es), color = "#fde725", linetype = "dashed", size = 2) +
  scale_fill_viridis_d() +
  scale_color_viridis_d() +
  geom_label(x=1.12, y=-1.5, label=label_control, color = 'white', fill = "#440154", fontface = "bold") +
  geom_label(x=1.12, y=1.5+sim_es, label=label_intervention, color = '#090c0e', fill = "#fde725", fontface = "bold") +
  scale_y_continuous(expand = c(0,0), breaks = c(-2:+2), labels = dv_scale) +
  ylab(dv_label) +
  xlab("count") +
  theme_modern() +
  theme(legend.position = "none",
        # panel.grid = element_blank(),
        # axis.ticks.x = element_line(color = "#666666"),
        # axis.line.x = element_line(color = "#666666"),
        # axis.line.y = element_blank(),
        # axis.title.y = element_blank(),
        # axis.title.x = element_blank(),
        axis.text.y = element_blank(),
        # axis.text.x = element_text(face = "bold")
        ) +
  coord_flip()

Dotplot

stat_dots()

ggplot(p_data, aes(x=x, group = group, fill = group, colour = group)) +
  stat_dots(alpha = .75) +
  geom_vline(aes(xintercept = 0), color = "#440154", linetype = "dashed", size = 2, alpha = .7) +
  geom_vline(aes(xintercept = sim_es+.01), color = "darkgrey", linetype = "dashed", size = 2, alpha = .7) +
  geom_vline(aes(xintercept = sim_es-.01), color = "darkgrey", linetype = "dashed", size = 2, alpha = .7) +
  geom_vline(aes(xintercept = sim_es), color = "#fde725", linetype = "dashed", size = 2) +  # color was #fde725
  geom_label(x=-2, y=.75, label=label_control, color = 'white', fill = "#440154", fontface = "bold") +
  geom_label(x=2.2, y=.75, label=label_intervention, color = '#090c0e', fill = "#fde725", fontface = "bold") +
  scale_fill_viridis_d() +
  scale_color_viridis_d() +
  scale_y_continuous(expand = c(0,0)) +
  scale_x_continuous(breaks = c(-2:+2), labels = dv_scale) +
  xlab(dv_label) +
  ylab("count") +
  theme_modern() +
  theme(legend.position = "none",
        # panel.grid = element_blank(),
        # axis.ticks.x = element_line(color = "#666666"),
        # axis.line.x = element_line(color = "#666666"),
        # axis.line.y = element_blank(),
        # axis.title.y = element_blank(),
        # axis.title.x = element_blank(),
        axis.text.y = element_blank(),
        # axis.text.x = element_text(face = "bold")
        )

Histograms

geom_histogram()

ggplot(data = p_data%>%dplyr::filter(group == "Without Intervention"), aes(x=x)) +
  geom_histogram(alpha = .8, fill = "#440154", color = "#440154") +
  geom_histogram(data = p_data%>%dplyr::filter(group == "With Intervention"), alpha = .7, fill = "#fde725", color = "#edd926") +
  geom_vline(aes(xintercept = 0), color = "#440154", linetype = "dashed", size = 2, alpha = .5) +
  geom_vline(aes(xintercept = sim_es), color = "#fde725", linetype = "dashed", size = 2) +
  scale_fill_viridis_d() +
  scale_color_viridis_d() +
  geom_label(x=-2, y=sim_n/15, label=label_control, color = 'white', fill = "#440154", fontface = "bold") +
  geom_label(x=2.2, y=sim_n/15, label=label_intervention, color = '#090c0e', fill = "#fde725", fontface = "bold") +
  scale_y_discrete(expand = c(0,0)) +
  scale_x_continuous(breaks = c(-2:+2), labels = dv_scale) +
  xlab(dv_label) +
  ylab("count") +
  theme_modern() +
  theme(legend.position = "none",
        # panel.grid = element_blank(),
        # axis.ticks.x = element_line(color = "#666666"),
        # axis.line.x = element_line(color = "#666666"),
        # axis.line.y = element_blank(),
        # axis.title.y = element_blank(),
        # axis.title.x = element_blank(),
        axis.text.y = element_blank(),
        # axis.text.x = element_text(face = "bold")
        )

Archive (old versions)

Normal Curve (using stat_function())

Visualization of Cohen’s \(U_3\) with two normal curves.

ggplot(data = data.frame(x = c(-3, 3)), aes(x)) +
  stat_function(fun = dnorm, n = 101, args = list(mean = 0, sd = 1), 
                # geom = "area", 
                # fill = "#d0d4d4", 
                color = "#d0d4d4",
                size = 2) + 
  geom_vline(aes(xintercept = 0), color = "#d0d4d4", linetype = "dotted", size = 2) +
  geom_label(x=-2, y=.3, label=label_control, color = 'white', fill = "#d0d4d4", fontface = "bold") +
  stat_function(fun = dnorm, n = 101, args = list(mean = sim_es, sd = 1),
                xlim = c(-3, 0),
                # geom = "area", 
                # fill = "#ff857a", 
                color = "#ff857a", 
                size = 2) +
  stat_function(fun = dnorm, n = 101, args = list(mean = sim_es, sd = 1),
                xlim = c(0, 3),
                geom = "area", 
                fill = "#cc6a61", 
                color = "#cc6a61", 
                alpha = .5,
                size = 2) + 
  geom_vline(aes(xintercept = sim_es), color = "white", linetype = "dotted", size = 1.5, alpha = .3) +
  geom_label(x=2.2, y=.3, label=label_intervention, color = 'white', fill = "#ff857a", fontface = "bold") +
  geom_label(x=1, y=.15, label="69%", color = 'white', fill = "#ff857a", fontface = "bold") +
  scale_x_continuous(breaks = c(-2:+2), labels = dv_scale) +
  scale_y_continuous(expand = c(0,0), limits = c(0,0.41)) +
  xlab(dv_label) +
  theme_minimal() +
  theme(panel.grid = element_blank(),
        axis.ticks.x = element_line(color = "#666666"),
        axis.line.x = element_line(color = "#666666"),
        axis.title.y = element_blank(),
        # axis.title.x = element_blank(),
        axis.text.y = element_blank(),
        axis.text.x = element_text(face = "bold"))

Normal Curve (using the simulated data) with “mean leabels”

p_nc2 <- 
ggplot(p_data, aes(x=x, group = group, fill = group, colour = group)) +
  # geom_dots() +
  # geom_vline(aes(xintercept = 0), color = "#440154", linetype = "dotted", size = 1.5) +
  geom_density(alpha = .6, size = 3) +
  # geom_vline(aes(xintercept = sim_es), color = "#fde725", linetype = "dotted", size = 1.5) +
  scale_fill_viridis_d() +
  scale_color_viridis_d() +
  geom_label(x=-2, y=.3, label=label_control, color = 'white', fill = "#440154", fontface = "bold") +
  geom_label(x=2.2, y=.3, label=label_intervention, color = '#090c0e', fill = "#fde725", fontface = "bold") +
  scale_y_continuous(expand = c(0,0)) +
  scale_x_continuous(breaks = c(-2:+2), labels = dv_scale, limits = c(-3,3.5)) +
  xlab(dv_label) +
  theme_modern() +
  theme(legend.position = "none",
        # panel.grid = element_blank(),
        # axis.ticks.x = element_line(color = "#666666"),
        # axis.line.x = element_line(color = "#666666"),
        axis.line.y = element_blank(),
        axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        axis.text.y = element_blank(),
        # axis.text.x = element_text(face = "bold")
        )

ggarrange(p_nc2, mean_label,
          ncol = 1,
          nrow = 2,
          heights = c(5,1.7),
          align = "v"
          )

geom_beeswarm()

Visualization of Cohen’s \(U_3\) with a beeswarm plot.

ggplot(p_data, aes(x=group, y = x, group = group, fill = group, colour = group)) +
  geom_hline(aes(yintercept = 0), color = "#440154", linetype = "solid", size = .5) +
  geom_beeswarm(cex = 1.6) +
  geom_hline(aes(yintercept = sim_es), color = "#fde725", linetype = "solid", size = .5) +
  scale_fill_viridis_d() +
  scale_color_viridis_d() +
  geom_label(x=1.4, y=-2, label=label_control, color = 'white', fill = "#440154", fontface = "bold") +
  geom_label(x=2.4, y=2, label=label_intervention, color = '#090c0e', fill = "#fde725", fontface = "bold") +
  scale_y_continuous(expand = c(0,0), breaks = c(-2:+2), labels = dv_scale) +  
  ylab(dv_label) +
  # theme_minimal() +
  theme_modern() +
  theme(legend.position = "none",
        # panel.grid = element_blank(),
        # axis.ticks.x = element_line(color = "#666666"),
        # axis.line.x = element_line(color = "#666666"),
        axis.line.y = element_blank(),
        axis.title.y = element_blank(),
        # axis.title.x = element_blank(),
        axis.text.y = element_blank(),
        # axis.text.x = element_text(face = "bold")
        ) +
  coord_flip()

stat_dots() with quantiles = 100

ggplot(p_data, aes(x=x, group = group, fill = group, colour = group)) +
  geom_vline(aes(xintercept = 0), color = "#440154", linetype = "solid", size = 1) +
  stat_dots(quantiles = 100, alpha = .4) +
  geom_vline(aes(xintercept = sim_es), color = "#fde725", linetype = "solid", size = 1) +
  scale_fill_viridis_d() +
  scale_color_viridis_d() +
  geom_label(x=-2, y=.3, label=label_control, color = 'white', fill = "#440154", fontface = "bold") +
  geom_label(x=2.2, y=.3, label=label_intervention, color = '#090c0e', fill = "#fde725", fontface = "bold") +
  scale_y_continuous(expand = c(0,0)) +
  scale_x_continuous(breaks = c(-2:+2), labels = dv_scale) +
  xlab(dv_label) +
  # theme_minimal() +
  theme_modern() +
  theme(legend.position = "none",
        # panel.grid = element_blank(),
        # axis.ticks.x = element_line(color = "#666666"),
        # axis.line.x = element_line(color = "#666666"),
        axis.line.y = element_blank(),
        axis.title.y = element_blank(),
        # axis.title.x = element_blank(),
        axis.text.y = element_blank(),
        # axis.text.x = element_text(face = "bold")
        )

stat_dotsinterval() with quantiles = 100

ggplot(p_data, aes(x=x, y = group, group = group, fill = group, colour = group)) +
  stat_dotsinterval(quantiles = 100) +
  scale_fill_viridis_d() +
  scale_color_viridis_d() +
  geom_label(x=-2, y=1.5, label=label_control, color = 'white', fill = "#440154", fontface = "bold") +
  geom_label(x=2.2, y=2.5, label=label_intervention, color = '#090c0e', fill = "#fde725", fontface = "bold") +
  scale_y_discrete(expand = c(0,0)) +
  scale_x_continuous(breaks = c(-2:+2), labels = dv_scale) +
  xlab(dv_label) +
  # theme_minimal() +
  theme_modern() +
  theme(legend.position = "none",
        # panel.grid = element_blank(),
        # axis.ticks.x = element_line(color = "#666666"),
        # axis.line.x = element_line(color = "#666666"),
        axis.line.y = element_blank(),
        axis.title.y = element_blank(),
        # axis.title.x = element_blank(),
        axis.text.y = element_blank(),
        # axis.text.x = element_text(face = "bold")
        )

stat_dots() different color

p_data %>%
ggplot(aes(x=x, y = group, group = group, fill = stat(x>0), colour = stat(x>0))) +
  stat_dots() +
  scale_fill_viridis_d() +
  scale_color_viridis_d() +
  # geom_label(x=-2, y=1.5, label=label_control, color = 'white', fill = "#440154", fontface = "bold") +
  # geom_label(x=2.2, y=2.5, label=label_intervention, color = '#090c0e', fill = "#fde725", fontface = "bold") +
  scale_y_discrete(expand = c(0,0)) +
  scale_x_continuous(breaks = c(-2:+2), labels = dv_scale) +
  xlab(dv_label) +
  # theme_minimal() +
  theme_modern() +
  theme(legend.position = "none",
        # panel.grid = element_blank(),
        # axis.ticks.x = element_line(color = "#666666"),
        # axis.line.x = element_line(color = "#666666"),
        axis.line.y = element_blank(),
        axis.title.y = element_blank(),
        # axis.title.x = element_blank(),
        axis.text.y = element_blank(),
        # axis.text.x = element_text(face = "bold")
        )

Pie Charts

Donut Plots

Percent of participants scoring higher than the control group’s average.

p_data %>%
  mutate(x0 = ifelse(x>0,1,0)) %>%
  group_by(group) %>%
  summarize(x0 = mean(x0)) %>%
  ggplot(aes(y = x0, x = group, group = group, fill = group, colour = group)) +
    geom_col() +
    coord_polar(theta = "y") +
    scale_y_reverse(limits = c(1,0), labels = NULL) +
    scale_x_discrete(limits = c(" ", "Without Intervention", "With Intervention")) +
    scale_fill_viridis_d() +
    scale_color_viridis_d() +
    geom_label(x=2.2, y=.085, label=label_control, color = 'white', fill = "#440154", fontface = "bold") +
    geom_label(x=3.1, y=.05, label=label_intervention, color = '#090c0e', fill = "#fde725", fontface = "bold") +
    theme_void() +
    theme(legend.position = "none",
          panel.grid = element_blank(),
          axis.ticks.x = element_blank(),
          axis.line.x = element_blank(),
          axis.title.y = element_blank(),
          axis.title.x = element_blank(),
          axis.text.y = element_blank(),
          axis.text.x = element_text(face = "bold"))

Waffle Charts

Percent of participants scoring higher than the control group’s average.

p_data %>%
  mutate(x0 = ifelse(x>0,1,0)) %>%
  group_by(group, x0) %>%
  summarize(vals = n()) %>%
  ggplot(aes(fill = as.factor(x0), values = vals)) +
    geom_waffle(n_rows = 10, radius = unit(4, "pt")) +
    scale_fill_viridis_d() +
    scale_color_viridis_d() +
    scale_x_continuous(labels = NULL) +
    coord_equal() +
    # geom_label(x=2.2, y=.085, label=label_control, color = 'white', fill = "#440154", fontface = "bold") +
    # geom_label(x=3.1, y=.05, label=label_intervention, color = '#090c0e', fill = "#fde725", fontface = "bold") +
    theme_void() +
    theme(legend.position = "none",
          panel.grid = element_blank(),
          axis.ticks.x = element_blank(),
          axis.line.x = element_blank(),
          axis.title.y = element_blank(),
          axis.title.x = element_blank(),
          axis.text.y = element_blank(),
          axis.text.x = element_text(face = "bold")) +
  facet_wrap(~group)